﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Workflow.Bussiness;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Workflow.Model;
using Portal.Workflow.Utilities;
using System.Web;

namespace Portal.Workflow.Render
{
    public class PlugInWorkflowRender
    {
        public static string GetJavascript(string SiteId, string ContentWorkFlowHolder,string MainFormHolder,bool VisibleMainFormHolder)
        {
            string JavascriptText =
                    " <script language=javascript>\r\n" +
                    "    function PreviewWorkflow(WorkflowDefId,WorkflowActionId,RecordId)\r\n" +
                    "    {\r\n" +
                    "        SiteId='" + SiteId + "';\r\n" +
                    "        AjaxOut=Portal.Workflow.Render.PlugInWorkflowRender.ServerSideProcessPreview(SiteId,WorkflowDefId,WorkflowActionId,RecordId).value;\r\n" +
                    "        if(AjaxOut.Error)\r\n" +
                    "        {\r\n" +
                    "            alert(AjaxOut.InfoMessage);\r\n" +
                    "            return;\r\n" +
                    "        }\r\n" +
                    "        window.open(AjaxOut.RetUrl,'_self');\r\n" +
                    "    }\r\n" +

                    "    function ProcessWorkflow(WorkflowDefId,WorkflowActionId,RecordId, LinkId)\r\n" +
                    "    {\r\n" +
                    "        SiteId='" + SiteId + "';\r\n" +

                    "        oReturnParam=Portal.Workflow.Bussiness.AjaxWorkflowTaskNextLink.ServerSideGetLinkUrl(SiteId, WorkflowDefId,WorkflowActionId, RecordId,  LinkId).value;\r\n" +
                    "        if(oReturnParam.IsError)\r\n" +
                    "        {\r\n" +
                    "           alert(oReturnParam.HtmlContent);\r\n" +
                    "           return;\r\n" +
                    "        }\r\n" +
                   

                    "        AjaxOut=Portal.Workflow.Render.PlugInWorkflowRender.ServerSideProcessAjaxNextLink(SiteId, WorkflowDefId, WorkflowActionId, RecordId, LinkId).value;\r\n" +

                    "        if(oReturnParam.IsLink)\r\n" +
                    "        {\r\n" +
                    //chay luon workflow




                    "           oDoNextLinkRetCls=Portal.Workflow.Bussiness.AjaxWorkflowTaskNextLink.ServerSideDoNextLink(SiteId, WorkflowDefId, WorkflowActionId, RecordId, LinkId, '', '').value;\r\n" +
                    "           if(oDoNextLinkRetCls.IsError==true)\r\n" +
                    "           {\r\n" +
                    "               alert(oDoNextLinkRetCls.ReturnMsg);\r\n" +
                    "               return;\r\n" +
                    "           }\r\n" +
                    //"           if(oDoNextLinkRetCls.IsLink)\r\n" +
                    //"           {\r\n" +
                    //"               window.open(oDoNextLinkRetCls.Url,'_self');\r\n" +
                    //"               return;\r\n" +
                    //"           }\r\n" +
                    //"           alert(oDoNextLinkRetCls.ReturnMsg);\r\n" +
                    //"           window.open(oDoNextLinkRetCls.Url,'_self');\r\n" +


                    "           window.open(oReturnParam.Url,'_self');\r\n" +
                    "           return;\r\n" +
                    "        }\r\n" +


                    "        document.getElementById('" + ContentWorkFlowHolder + "').innerHTML=AjaxOut.HtmlContent;\r\n" +
                    "        document.getElementById('" + ContentWorkFlowHolder + "').style.display='block';\r\n";
            if (VisibleMainFormHolder)
            {
                JavascriptText +=
                    "        document.getElementById('" + MainFormHolder + "').style.display='none';\r\n";
            }
            JavascriptText +=
                    "    }\r\n" +
                    " </script>\r\n" +
                    WorkflowBll.ServerSideGetPlugInJavascriptDoNextLink(SiteId, ContentWorkFlowHolder, MainFormHolder) +
                    WorkflowBll.ServerSideGetWorkflowHistoryJavascript();
            return JavascriptText;
        }



        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideProcessAjaxNextLink(
            string SiteId,
            string WorkflowDefId,
            string WorkflowActionId,
            string RecordId,
            string LinkId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                new WorkflowBll().CheckDoNextLinkRunAtServer(SiteId, WorkflowActionId);
                string Html = new AjaxWorkflowTaskNextLink().Draw(SiteId, WorkflowDefId, WorkflowActionId, RecordId, LinkId);
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideProcessPreview(
            string SiteId,
            string WorkflowDefId,
            string WorkflowActionId,
            string RecordId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                WorkFlowRecordCls OWorkFlowRecord = WorkflowBll.CreateWorkFlowRecord(OSiteParam.ConnectionString, WorkflowActionId);
                IWorkflowPreview FoundWorkflowPreview = WorkflowPreviewUrl.Find(OSiteParam.DllPath, OWorkFlowRecord.ExtraInfo);
                if (FoundWorkflowPreview != null)
                {
                    WorkflowPreviewParam OWorkflowPreviewParam = new WorkflowPreviewParam();
                    OWorkflowPreviewParam.ObjectId = RecordId;
                    OWorkflowPreviewParam.WorkflowDefId = OWorkFlowRecord.WorkFlowDefId;
                    OWorkflowPreviewParam.WorkflowActionId = OWorkFlowRecord.Id;
                    OWorkflowPreviewParam.ObjectType = OWorkFlowRecord.ExtraInfo;
                    OWorkflowPreviewParam.ExtraData = OWorkFlowRecord.ExtraData;
                    RetWorkflowPreviewParam ORetWorkflowPreviewParam = FoundWorkflowPreview.GetPreviewUrl(SiteId, OWorkflowPreviewParam);
                    ORetWorkflowPreviewParam.Url = GetUrlDetails("details", RecordId, OSiteParam);
                    OAjaxOut.RetUrl =ORetWorkflowPreviewParam.Url;
                    OAjaxOut.RetUrlTarget=ORetWorkflowPreviewParam.Target;
                }
                else
                {
                    throw new Exception("Không tìm thấy dịch vụ xem chi tiết!");
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        public static string GetUrlDetails( string Subject, string CmsDataId, SiteParam OSiteParam)
        {
            Subject = Portal.Environment.Environments.ConvertSubject(Subject);
            string HttpDataRoot = WebConfig.GetConfigFromXml(OSiteParam, "HttpAdminRoot");            
            string Url = HttpDataRoot + "/" + Subject + "-nd-" + CmsDataId + "." + Portal.Environment.Environments.Extension;            
            return Url;
        }
    }
}
